﻿@model ContentItemEditModel
@{
    Layout = "_ControlPanel";
    bool HaveAdditional = Model != null && Model.AdditionalProperties != null;
    Html.IncludeScript("~/Scripts/tinymce/jquery.tinymce.js");
    Html.IncludeScript("~/Scripts/tunynet/jquery.tn.htmlEditor.unobtrusive.js");
    Html.IncludeScript("~/Scripts/tunynet/jquery.tn.textarea.js");
    int summaryCount = Model.Summary == null ? 0 : HtmlUtility.StripHtml(Model.Summary, true, false).TrimEnd('.').Length;
    if (summaryCount > 200)
    {
        summaryCount = 200;
    }
    int TitleCount = Model.Title == null ? 0 : Model.Title.Length;
    IUser currentUser = UserContext.CurrentUser;
    ContentItem item = ViewData.Get<ContentItem>("contentItem", new ContentItem());
    string author = string.IsNullOrEmpty(item.Author) ? currentUser.DisplayName : item.Author;
    if (HaveAdditional && !string.IsNullOrEmpty(Model.AdditionalProperties.Get<string>("OriginalAuthor", string.Empty)))
    {
        author = Model.AdditionalProperties.Get<string>("OriginalAuthor", string.Empty);
    }

    string highlightStyle = string.Empty;
    if (HaveAdditional && !string.IsNullOrEmpty(Model.AdditionalProperties.Get<string>("Color", string.Empty)))
    {
        string color = Model.AdditionalProperties.Get<string>("Color", string.Empty);
        if (!string.IsNullOrEmpty(color))
        {
            highlightStyle = string.Format("color:{0};", color);
        }
    }
    if (HaveAdditional && !string.IsNullOrEmpty(Model.AdditionalProperties.Get<bool>("isBold", false).ToString()))
    {
        bool isBold = Model.AdditionalProperties.Get<bool>("IsBold", false);
        if (isBold)
        {
            highlightStyle += "font-weight:bold;";
        }
    }
}

<div class="tn-block tnc-ctnborder">
    <div class="tnc-main">
        <div class="tnc-ctn-center">
            <h1>
                <span>@(Model == null || Model.ContentItemId == 0 ? "发布文章" : "编辑文章")</span>
            </h1>
            @using (Html.BeginForm("EditContentItem", "ControlPanelCms", FormMethod.Post, new { id = "form-News-edit" }))
            {
                @Html.HiddenFor(n => n.ContentItemId)
                @Html.HiddenFor(n => n.ContentTypeId)
                <div class="tnc-ctnright-list">
                    <div class="tn-publish">
                        <div class="tn-write-title">
                            <div class="tnc-formbtn-box">
                                @Html.TextBoxFor(n => n.Title, InputWidthTypes.Longest, new RouteValueDictionary { { "class", "tn-textbox tn-border-gray tn-input-longest tn-input-ruler" }, { "id", "Title" }, { "style", @highlightStyle } })

                                <a id="TitleTextColor" href="javascript:void(0)" class="tn-button tn-corner-all tn-button-secondary tn-button-hollow tn-button-icon-only"></a>
                                <a href="javascript:void(0)" id="TitleTextWeight" class="tnc-formbtn-b"></a>
                                @Html.Hidden("Color", HaveAdditional ? Model.AdditionalProperties.Get<string>("Color", string.Empty) : string.Empty)
                                @Html.Hidden("IsBold", HaveAdditional && Model.AdditionalProperties.Get<bool>("IsBold", false))
                                <span id="TitleCount" class="tn-count tn-text-note">@TitleCount/@TextLengthSettings.TEXT_SUBJECT_MAXLENGTH</span>
                                @Html.ValidationMessageFor(n => n.Title)
                            </div>
                        </div>
                        <div class="tn-editor">
                            @Html.HtmlEditor("Body", HaveAdditional ? Model.AdditionalProperties.Get<string>("Body", string.Empty) : "", new HtmlEditorOptions(HtmlEditorMode.Enhanced)
                                            .MergeHtmlAttribute("style", "height:400px;")
                                            .MergeHtmlAttribute("class", "{required:true,maxlength:" + TextLengthSettings.TEXT_BODY_MAXLENGTH + ", messages:{required:'请填写内容',maxlength:'最长不能超过" + TextLengthSettings.TEXT_BODY_MAXLENGTH + "个字符'}}")
                                            .AddPhotoButton(TenantTypeIds.Instance().ContentItem(), Model.ContentItemId).SetShowPagebreak(true)
                                            .AddUploadFileButton())
                            @Html.ValidationMessage("Body")
                        </div>
                        <div class="tn-form-box">
                            <table>
                                <tbody>
                                    <tr>
                                        <th>栏目：
                                        </th>
                                        <td>
                                            @Html.FolderDropDownListFor(m => m.ContentFolderId, ContentTypeKeys.Instance().News())
                                            @Html.ValidationMessageFor(n => n.ContentFolderId)
                                        </td>
                                    </tr>
                                    <tr>
                                        <th>标签：
                                        </th>
                                        <td>
                                            @Html.AjaxAction("_TagSelector", "ChannelCms", new { contentItemId = Model.ContentItemId, contentFolderId = Model.ContentFolderId })
                                        </td>
                                    </tr>
                                    <tr>
                                        <th>来源：
                                        </th>
                                        <td>
                                            @Html.TextBox("CopyFrom", HaveAdditional ? Model.AdditionalProperties.Get<string>("CopyFrom", string.Empty) : "", InputWidthTypes.Medium, new RouteValueDictionary { { "watermark", "名称" } })
                                            @Html.ValidationMessage("CopyFrom")

                                            @Html.TextBox("CopyFromUrl", HaveAdditional ? Model.AdditionalProperties.Get<string>("CopyFromUrl", string.Empty) : "", InputWidthTypes.Long, new RouteValueDictionary { { "watermark", "http://www.example.com" }, { "class", "url" } })
                                            @Html.ValidationMessage("CopyFromUrl")
                                        </td>
                                    </tr>
                                    <tr>
                                        <th>作者：
                                        </th>
                                        <td>
                                            @Html.TextBox("OriginalAuthor", author, InputWidthTypes.Medium)
                                            @Html.ValidationMessage("OriginalAuthor")
                                        </td>
                                    </tr>
                                    <tr>
                                        <th>责任编辑：
                                        </th>
                                        <td>
                                            @Html.TextBox("Editor", HaveAdditional ? Model.AdditionalProperties.Get<string>("Editor", string.Empty) : Model.ContentFolder.Editor, InputWidthTypes.Medium)
                                            @Html.ValidationMessage("Editor")
                                        </td>
                                    </tr>
                                    <tr>
                                        <th>摘要：
                                        </th>
                                        <td>
                                            @Html.TextAreaFor(n => n.Summary, InputWidthTypes.Longest, 2, 2, new RouteValueDictionary { { "id", "Summary" } })
                                            <span id="SummaryCount" class="tn-count tn-text-note">@summaryCount/@TextLengthSettings.TEXT_DESCRIPTION_MAXLENGTH</span>
                                            @Html.ValidationMessage("Summary")
                                        </td>
                                    </tr>
                                    <tr>
                                        <th></th>
                                        <td>
                                            @Html.SipmleCheckBox("TrimBodyAsSummary", true, !HaveAdditional || Model.AdditionalProperties.Get<bool>("TrimBodyAsSummary", true), new { id = "TrimBodyAsSummary" })
                                            @Html.LabelFor(n => n.TrimBodyAsSummary, "截取内容前200字作为摘要")
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                <div class="tn-collapsible tn-collapsible-closed">
                    <div class="tn-border-bottom tn-border-gray tn-collapsible-header">
                        <h4><a href="javascript:void(0)">更多</a></h4>
                    </div>
                    <div class="tn-collapsible-content tn-border-bottom tn-border-gray tn-border-dashed"
                        style="display: block;">
                        <div class="tn-form-box">
                            <table>
                                <tr>
                                    <th>标题图：
                                    </th>
                                    <td>
                                        @Html.SetTitleImageFor(n => n.FeaturedImageAttachmentId, TenantTypeIds.Instance().ContentItem(), Model.ContentItemId)
                                    </td>
                                </tr>
                                <tr>
                                    <th></th>
                                    <td>
                                        @Html.SipmleCheckBox("FirstAsTitleImage", true, HaveAdditional && Model.AdditionalProperties.Get<bool>("FirstAsTitleImage", true), new { id = "FirstAsTitleImage" })
                                        @Html.LabelFor(n => n.FirstAsTitleImage, "设置上传的第一张图为标题图")
                                    </td>
                                </tr>
                                <tr>
                                    <th>分页方式：
                                    </th>
                                    <td>
                                        <ul class="tn-checkbox-list" style="vertical-align: top">
                                            <li class="tn-inline-list">
                                                @Html.SipmleCheckBox("AutoPage", true, HaveAdditional && Model.AdditionalProperties.Get<bool>("AutoPage", false), new { id = "AutoPage" })
                                                @Html.LabelFor(n => n.AutoPage, "自动分页")
                                            </li>
                                        </ul>
                                        <span id="pageL">
                                            @Html.TextBox("PageLength", HaveAdditional ? Model.AdditionalProperties.Get<string>("PageLength", string.Empty) : "1000", InputWidthTypes.Short, new RouteValueDictionary { { "class", "digits" } })
                                            @Html.ValidationMessage("PageLength")
                                            <span class="tn-text-note">字符数（包含HTML标记）</span>
                                        </span>
                                    </td>
                                </tr>
                                <tr>
                                    <th>发布时间：
                                    </th>
                                    <td>
                                        @Html.DateTimePicker("ReleaseDate", new DateTimePicker().SetShowTime(true).SetMinDate("-10Y")
                                        .SetSelectedDate(Model.ReleaseDate))
                                    </td>
                                </tr>
                                <tr>
                                    <th>置顶设置：
                                    </th>
                                    <td>
                                        @Html.CheckBoxFor(n => n.IsGlobalSticky, new { @class = "tn-checkbox" })
                                        @Html.LabelFor(n => n.IsGlobalSticky, "整站置顶")
                                        @SetStickyTime(Model.GlobalStickyDate)
                                    </td>
                                </tr>
                                <tr>
                                    <th></th>
                                    <td>
                                        @Html.CheckBoxFor(n => n.IsFolderSticky, new { @class = "tn-checkbox" })
                                        @Html.LabelFor(n => n.IsFolderSticky, "栏目置顶")
                                        @SetStickyTime2(Model.FolderStickyDate)
                                    </td>
                                </tr>
                                <tr>
                                    <th>允许评论：
                                    </th>
                                    <td>
                                        @{
                bool enableComment = !HaveAdditional || Model.AdditionalProperties.Get<bool>("EnableComment", true);
                                        }
                                        <ul class="tn-radiobutton-list">
                                            <li class="tn-inline-list">
                                                @Html.RadioButton("EnableComment", true, enableComment, new { id = "EnableComment-true", @class = "tnui-type" })
                                                <label for="EnableComment-true">
                                                    允许</label>&nbsp;&nbsp;
                                                @Html.RadioButton("EnableComment", false, !enableComment, new { id = "EnableComment-false", @class = "tnui-type" })
                                                <label for="EnableComment-false">
                                                    不允许</label>
                                                @Html.ValidationMessage("EnableComment")
                                            </li>
                                        </ul>
                                    </td>
                                    <td></td>
                                </tr>
                            </table>
                        </div>
                    </div>
                </div>
                <div class="tn-publish-btn tn-publish-btn-center">
                    @Html.Button("发布", ButtonTypes.Submit, HighlightStyles.Primary, ButtonSizes.Large, null, TextIconLayout.IconText, null, new { id = "submit-publish" })
                    @Html.Button("取消", ButtonTypes.Button, HighlightStyles.Secondary, ButtonSizes.Large, url: SiteUrls.Instance().CmsControlPanelManage())
                </div>
            }
        </div>
    </div>
</div>
@helper SetStickyTime(DateTime StickyTime)
{
    <text>
    <div @(!Model.IsGlobalSticky ? "style=display:none" : "") id="settime-stickyTime">
        <div id="time-stickyTime-show" @(StickyTime.CompareTo(DateTime.UtcNow) <= 0 ? "style=display:none" : "")>
            置顶截止时间： @StickyTime.ToFriendlyDate(false, "yyyy-MM-dd") <a id="time-edit">编辑</a>
        </div>
        <div id="time-stickyTime-edit" @(StickyTime.CompareTo(DateTime.UtcNow) > 0 ? "style=display:none" : "")>
            置顶截止时间： @Html.DatePicker("GlobalStickyDate", DateTime.Now.AddDays(3), "0", null)
        </div>
    </div>
    <script type="text/javascript" language="javascript">
        $("#time-edit").click(function (e) {
            e.preventDefault();
            $("#time-stickyTime-edit").show();
            $("#time-stickyTime-show").hide();
        });
    </script>
    </text>
}

@helper SetStickyTime2(DateTime StickyTime2)
{
    <text>
    <div @(!Model.IsFolderSticky ? "style=display:none" : "") id="settime-stickyTime2">
        <div id="time-stickyTime-show2" @(StickyTime2.CompareTo(DateTime.UtcNow) <= 0 ? "style=display:none" : "")>
            置顶截止时间： @StickyTime2.ToFriendlyDate(false, "yyyy-MM-dd") <a id="time-edit2">编辑</a>
        </div>
        <div id="time-stickyTime-edit2" @(StickyTime2.CompareTo(DateTime.UtcNow) > 0 ? "style=display:none" : "")>
            置顶截止时间： @Html.DatePicker("FolderStickyDate", DateTime.Now.AddDays(3), "0", null)
        </div>
    </div>
    <script type="text/javascript" language="javascript">
        $("#time-edit2").click(function (e) {
            e.preventDefault();
            $("#time-stickyTime-edit2").show();
            $("#time-stickyTime-show2").hide();
        });
    </script>
    </text>
}
<script language="javascript" type="text/javascript">
    $(document).ready(function () {
        //点击置顶
        $("input[id='IsGlobalSticky'][type='checkbox']").click(function () {
            $("#settime-stickyTime").toggle();
        });
        $("input[id='IsFolderSticky'][type='checkbox']").click(function () {
            $("#settime-stickyTime2").toggle();
        });
     
        $("#pageL").hide();
        $("#AutoPage").click(function () {
            if (!!$("#AutoPage").attr("checked")){
                $("#pageL").show();                 
                var value= $("#PageLength").data("currentValue");
                if(!value)
                    value=500;
                $("#pageLength").val(value);
            }
            else {
                $("#pageL").hide();                
                $("#pageLength").data("currentValue",$("#pageLength").val());
                $("#pageLength").val("");
            }
        });
        //文字颜色
        $("#TitleTextColor").bigColorpicker(function (el, color) {

            $("#Title").css("color", color);
            $("#Color").val(color);
        });

        //文字加粗
        $('#TitleTextWeight').click(function () {
            if ($(this).hasClass("tn-selected")) {
                $(this).removeClass("tn-selected")
                $('#Title').css('font-weight', 'normal');
                $("#IsBold").val(false);
            }
            else {
                $(this).addClass("tn-selected")
                $('#Title').css('font-weight', 'bold');
                $("#IsBold").val(true);
            }
        });
        var form = $('form#form-News-edit');
        //截字
        $("#Title", form).GetTextCount('TitleCount', @(TextLengthSettings.TEXT_SUBJECT_MAXLENGTH), true);
        $("#Summary", form).GetTextCount('SummaryCount', @(TextLengthSettings.TEXT_DESCRIPTION_MAXLENGTH), true, true);

        //截取内容前200字为摘要
        function trimBodyAsSummaryClicked() {
            if($("#TrimBodyAsSummary:checked").length)
            { 
                $("#Summary").val("");
                $("#Summary").attr("disabled",true);
            }
            else
                $("#Summary").attr("disabled",false);
        }
        trimBodyAsSummaryClicked();
        $("#TrimBodyAsSummary").click(trimBodyAsSummaryClicked);
        //点击"发布
        $("#submit-publish",form).click(function(e){
            e.preventDefault();
            if (form.valid()){
                form.submit();
            }
        })
        //表单有录入内容但未保存时，用户离开页面提示
        $('form').enable_changed_form_confirm("");
      
    });
</script>
